package com.suixinsanren.design.state.demo.demo04;

public class GumballMachine {

    private State hasQuarterState = new HasQuarterState(this);
    private State noQuarterState = new NoQuarterState(this);
    private State soldState = new SoldState(this);
    private State soldOutState = new SoldOutState(this);
    private State winnerState = new WinnerState(this);

    private State currentState = soldOutState;
    private int count;

    public GumballMachine(int numberGumballs) {
        this.count = numberGumballs;
        if (count > 0) {
            this.currentState = noQuarterState;
        }
    }

    public void insertQuarter() {
        currentState.insertQuarter();
    }

    public void ejectQuarter() {
        currentState.ejectQuarter();
    }

    public void turnCrank() {
        currentState.turnCrank();
        currentState.dispense();
    }

    public void refill(int numberGumball) {
        currentState.refill(numberGumball);
    }

    public void releaseBall() {
        count--;
    }


    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public void setHasQuarterState(State hasQuarterState) {
        this.hasQuarterState = hasQuarterState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public void setNoQuarterState(State noQuarterState) {
        this.noQuarterState = noQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public void setSoldState(State soldState) {
        this.soldState = soldState;
    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public void setSoldOutState(State soldOutState) {
        this.soldOutState = soldOutState;
    }

    public State getWinnerState() {
        return winnerState;
    }

    public void setWinnerState(State winnerState) {
        this.winnerState = winnerState;
    }

    public State getCurrentState() {
        return currentState;
    }

    public void setCurrentState(State currentState) {
        this.currentState = currentState;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "GumballMachine{" +
                "当前状态=" + currentState +
                ", 当前糖果个数=" + count +
                '}';
    }
}
